package com.mune.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mune.gulimall.product.entity.ProductAttrValueEntity;
import com.mune.gulimall.product.service.ProductAttrValueService;
import com.mune.gulimall.product.vo.AttrRespVo;
import com.mune.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.mune.gulimall.product.entity.AttrEntity;
import com.mune.gulimall.product.service.AttrService;
import com.mune.common.utils.PageUtils;
import com.mune.common.utils.R;


/**
 * 商品属性
 *
 * @author mune
 * @email 1356008591@qq.com
 * @date 2020-07-28 20:48:31
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
  @Autowired
  private AttrService attrService;
  @Autowired
  private ProductAttrValueService attrValueService;

  @GetMapping("/base/listforspu/{spuId}")
  public R baseAttrListForSpu(@PathVariable("spuId") Long spuId) {
    List<ProductAttrValueEntity> entities = attrValueService.baseAttrListForSpu(spuId);
    return R.ok().put("data", entities);
  }

  @GetMapping("/{attrType}/list/{categoryId}")
  public R baseAttrList(@RequestParam Map<String, Object> params, @PathVariable("categoryId") Long categoryId,
                        @PathVariable("attrType") String type) {
    PageUtils pageUtils = attrService.queryBaseAttrPage(params, categoryId, type);
    return R.ok().put("page", pageUtils);
  }

  /**
   * 列表
   */
  @RequestMapping("/list")
  public R list(@RequestParam Map<String, Object> params) {
    PageUtils page = attrService.queryPage(params);

    return R.ok().put("page", page);
  }


  /**
   * 信息
   */
  @RequestMapping("/info/{attrId}")
  public R info(@PathVariable("attrId") Long attrId) {
    AttrRespVo attr = attrService.getAttrInfo(attrId);
    return R.ok().put("attr", attr);
  }

  /**
   * 保存
   */
  @RequestMapping("/save")
  public R save(@RequestBody AttrVo attr) {
    attrService.saveAttr(attr);

    return R.ok();
  }

  /**
   * 修改
   */
  @RequestMapping("/update")
  public R update(@RequestBody AttrVo attr) {
    attrService.updateAttr(attr);

    return R.ok();
  }

  @PostMapping("/update/{spuId}")
  public R updateSpuAttr(@PathVariable("spuId") Long spuId, @RequestBody List<ProductAttrValueEntity> entities) {

    attrValueService.updateSpuAttr(spuId, entities);

    return R.ok();
  }

  /**
   * 删除
   */
  @RequestMapping("/delete")
  public R delete(@RequestBody Long[] attrIds) {
    attrService.removeByIds(Arrays.asList(attrIds));

    return R.ok();
  }

}
